import {
  defineConfig,
  loadEnv
} from 'vite'
import vue from '@vitejs/plugin-vue'
import viteCompression from 'vite-plugin-compression'
const path = require('path')

// https://vitejs.dev/config/
export default defineConfig(({mode, command}) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env
  return {
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src')
      }
    },
    base: VITE_APP_ENV === 'production' ? '/' : '/', //路由去#
    server: {
      port: 82,
      host: true,
      open: true,
      https: false, // 是否使用https协议
      proxy: {
        '/api': { // 跨域代理，跟webpack的vue.config.js差不多，若有需要可放开根据实际进行配置
          target: 'http://xxx.xxxxx.xxx/',
          changeOrigin: true,
          rewrite: path => path.replace(/^\/api/, '')
        }
      }
    },
    plugins: [
      vue(),
      viteCompression({
        threshold: 500
      })
    ],
    build: {
      // 清除console和debugger
      terserOptions: {
        compress: {
          drop_console: true,
          drop_debugger: true,
        },
      }
    },
    css: {
      postcss: {
        plugins: [
          {
            postcssPlugin: 'internal:charset-removal',
            AtRule: {
              charset: (atRule) => {
                if (atRule.name === 'charset') {
                  atRule.remove();
                }
              }
            }
          }
        ]
      }
    }
  }
})